﻿using System;
using System.Configuration;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xenta.Utils;

namespace IisHost.UI
{
    public partial class LoginControl : UserControl
    {
        public void BindData()
        {
            lblMessage.Visible = false;
        }

        protected void OnCommand(object sender, CommandEventArgs e)
        {
            lblMessage.Visible = false;

            switch(e.CommandName)
            {
                case "Login":
                    {
                        string password = txtPassword.Text.Trim();
                        if(!String.IsNullOrWhiteSpace(password))
                        {
                            string passwordHash1 = CryptoUtility.SHA1Hash(password)
                                .ToHexString();
                            string passwordHash2 = ConfigurationManager.AppSettings["Password"];
                            if(passwordHash1.Equals(passwordHash2))
                            {
                                FormsAuthentication.SetAuthCookie("admin", false);
                                Response.Redirect("~/");
                            }
                        }
                        lblMessage.Visible = true;
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = "Login Failed";
                    }
                    break;
            }
        }
    }
}